package com.mall.b2c.controller;

import com.mall.b2c.domain.delivery.DeliveryMethodDomain;
import com.mall.b2c.enums.IntergralLogType;
import com.mall.b2c.service.DeliveryMethodService;
import com.mall.b2c.service.IntergralLogService;
import com.mall.b2c.service.IntergralOrderService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Message;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.enums.OperateType;
import com.mall.common.exception.NoticeException;
import com.mall.common.log.Log;
import com.mall.common.util.EnumUtils;
import com.mall.common.util.ResponseUtils;
import com.mall.common.util.StringUtil;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chen on 16/12/15.
 */
@Controller
@RequestMapping("/admin/b2c/b2c_intergral_order")
public class IntergralOrderController extends BaseController {
    @Resource
    IntergralOrderService intergralOrderService;

    @Resource
    DeliveryMethodService deliveryMethodService;
    /**
     * 跳转到列表页面
     */
    @RequestMapping(value="/list", method= RequestMethod.GET)
    public String list(Model model){

        return "/admin/b2c/intergralOrder/list"
                ;
    }
    /**
     * 跳转到列表页面
     */
    @RequestMapping(value="/statisticslist", method= RequestMethod.GET)
    public String statisticslist(Model model){

        return "/admin/b2c/intergralOrderStatistics/list"
                ;
    }
    /**
     * 获取列表信息
     */
    @RequestMapping(value="/queryData", method=RequestMethod.POST)
    @ResponseBody
    public Pager queryData(Pager pager, HttpServletRequest request){
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
        pager = intergralOrderService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }

    @RequestMapping(value="/queryStatisticsData", method=RequestMethod.POST)
    @ResponseBody
    public Pager querySumData(HttpServletRequest request, String startDate, String endDate){
       //Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, Object>> list = null;
        try {
            Date starDate = null;
            if(StringUtil.isNotNull(startDate)){
                starDate = sdf.parse(startDate);
            }
            Date enDate = null;
            if(StringUtil.isNotNull(endDate)){
                enDate = sdf.parse(endDate);
            }



            list = intergralOrderService.findList(starDate, enDate);
        }catch (ParseException e){
          //  e.printStackTrace();
            throw  new NoticeException("系统错误");
        }
        Pager page = new Pager();
        page.setList(list);
        page.setPageSize(1000);
        page.setTotalCount(1000);
        return page;
    }


    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String send(Long id, Model model) {
//      return  intergralOrderService.send(id).getContent();
        model.addAttribute("order", intergralOrderService.find(id));
        List<DeliveryMethodDomain> deliveryMethodList = deliveryMethodService.getAll();
        model.addAttribute("deliveryMethodList", deliveryMethodList);
        return "/admin/b2c/intergralOrder/input";

    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(Long id, Long deliveryMethodId, String express, HttpServletResponse response) {
        DeliveryMethodDomain methodDomain = deliveryMethodService.find(deliveryMethodId);
        return ResponseUtils.renderJSON(intergralOrderService.send(id, methodDomain, express), response);
    }


    @RequestMapping(value="/received", method=RequestMethod.POST)
    @ResponseBody
    public String received(Long id, HttpServletResponse response) {
    //  return  intergralOrderService.received(id).getContent();
        return ResponseUtils.renderJSON(intergralOrderService.received(id), response);
    }


}
